
package com.joysuch.wwyt.core.service.impl;

import java.util.Date;

import com.joysuch.wwyt.core.common.util.SimpleDateFormatCache;
import com.joysuch.wwyt.core.entity.BaseBizCodeSeed;
import com.joysuch.wwyt.core.enums.BizPoints;
import com.joysuch.wwyt.core.repository.BaseBizCodeSeedDao;
import com.joysuch.wwyt.core.service.BizCodeGenerator;
import com.joysuch.wwyt.core.support.Context;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class BizCodeDatabaseGenerator implements BizCodeGenerator {

    @Autowired
    private BaseBizCodeSeedDao seedDao;

    @Override
    @Transactional
    public String generate(BizPoints bizPoint) {
        BaseBizCodeSeed seedBean = seedDao.findByBizPoint(bizPoint.getCode());
        if (seedBean == null) {
            throw new IllegalStateException("业务类型#" + bizPoint.getCode() + ":" + bizPoint.getName() + " 不存在");
        }
       Long seed = seedDao.getAndIncrementSeed(bizPoint.getCode(), Context.getCurrentTenantId(),
                Context.getCompanyCode());

        log.info("... BizCodeDatabaseGenerator: code={},tenentid={},companyCode={}",bizPoint.getCode(), Context.getCurrentTenantId(),
                Context.getCompanyCode());
        return create(seedBean, seed);
    }

    protected String create(BaseBizCodeSeed seedBean, Long seed) {
        String no = StringUtils.leftPad(String.valueOf(seed), seedBean.getLength(), '0');
        if (seedBean.getType() == 0) {
            return seedBean.getPrefix() + no;
        } else if (seedBean.getType() == 1) {
            return seedBean.getPrefix() + date() + no;
        } else {
            throw new IllegalArgumentException("尚不支持的种子类型：" + seedBean.getType());
        }
    }

    private String date() {
        return SimpleDateFormatCache.getYmd_Pure().format(new Date());
    }

    @Override
    @Transactional
    public String generate(BizPoints bizPoint, Long tenentId, String orgCode) {
        BaseBizCodeSeed seedBean = seedDao.findByBizPointAndTenentIdAndOrgCode(bizPoint.getCode(), tenentId, orgCode);
        if (seedBean == null) {
            throw new IllegalStateException("业务类型#" + bizPoint.getCode() + ":" + bizPoint.getName() + " 不存在");
        }
        Long seed = seedDao.getAndIncrementSeed(bizPoint.getCode(), tenentId, orgCode);
       // Long seed = generateSeed(seedBean);
        return create(seedBean, seed);
    }



//    private Long generateSeed(BaseBizCodeSeed seedBean) throws IllegalStateException {
//        int count = seedDao.updateBizCodeSeed(seedBean.getId(),seedBean.getVersion());
//        log.info("... generateSeed id={}, version={}, count={} " , seedBean.getId(), seedBean.getVersion(), count);
//        int index = 0;
//        while(count ==0){
//            seedBean =seedDao.findById(seedBean.getId()).orElse(null);
//            log.info("... generateSeed  id={}, seed={}" , seedBean.getId(), seedBean.getSeed());
//            count = seedDao.updateBizCodeSeed(seedBean.getId(),seedBean.getVersion());
//            index++;
//            if(index >10){
//              throw new IllegalStateException("业务类型#" + seedBean.getPrefix() + " 生成新出现异常") ;
//            }
//        }
//        log.info("... generateSeed  return id={}, seed={}" , seedBean.getId(), seedBean.getSeed());
//        return seedBean.getSeed();
//    }

}
